﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Royalsoft.Common;

namespace NJL.Web.AdminEnterprise
{
    public partial class AddEnterPriseService :BasePage
    {
        NJL.BLL.NJL_COMPANY_SERVICE bll = new BLL.NJL_COMPANY_SERVICE();
        NJL.Model.NJL_COMPANY_SERVICE model;
        NJL.BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }
        }
        void bind()
        {
            DataSet ds = bllServiceType.GetList("");
            rblServiceType.DataSource = ds;
            rblServiceType.DataTextField = "TYPE_NAME";
            rblServiceType.DataValueField = "TYPE_CODE";
            rblServiceType.DataBind();

            for (var i = 1; i <=5; i++)
            {
                rblServiceLevel.Items.Add(new ListItem(""+i, ""+i));                            //服务等级
            }
 
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            NJL.Model.NJL_COMPANY modelCompany = bllCompany.GetCompanyByUserIDModel(UserId);    //根据用户编号查询企业编号  
            model = new Model.NJL_COMPANY_SERVICE();       
            model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            if (FileUpload1.HasFile)
            {
                model.IMAGE_CODE = SaveUploadFile(Request.Files);
            }          
            model.NJL_COMPANY_ID = modelCompany.ID;
            model.TYPE_CODE = rblServiceType.SelectedValue;
            model.SERVICE_LEVEL = Convert.ToInt32(rblServiceLevel.SelectedValue);
            model.SERVICE_NAME = PageValidate.InputText(txtAttractionsTitle.Text.Trim(),50);
            model.SERVICE_CONTENT = txtContent.Text.Trim();
            model.SERVICE_INTRO = txtDesc.Text.Trim();
            model.TAG = PageValidate.InputText(txtTag.Text.Trim(),100);
            model.CREATE_DATE_TIME = DateTime.Now;
            if (bll.Add(model))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "恭喜您，发表成功", "EnterPriseService.aspx");
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "5";  //服务5
                            modelTag.USED_COUNT = 0;
                            modelTag.AREA_CODE = modelCompany.AREA_CODE;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion 
            }
        }
    }
}